﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// 文件名：ClsRemoveProduct.cs
/// 作  者：Kidy
/// 时  间：2006.11.26
/// 说  明：ClsRemoveProduct类用于调拨管理信息相关处理
public class ClsRemoveProductRDAL
{
    /// <summary>
    /// 数据库连接字符串
    /// </summary>
    private static string strSqlConnectionString = ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"];

    /// <summary>
    /// 获取所有产品调拔主表信息
    /// </summary>
    /// <param name="Co_ID">公司ID</param>
    /// <param name="User_ID">用户ID</param>
    /// <param name="Ds_ID">数据集ID</param>
    /// <returns>返回退货主表信息数据集</returns>
    public DataSet GetRemoveProductMains(string Co_ID, string User_ID, string Ds_ID)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_GetDataSetRemoveM", con);

        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        //dat.SelectCommand.Parameters.Add("@co_id", Co_ID);
        //dat.SelectCommand.Parameters.Add("@user_id", User_ID);
        //dat.SelectCommand.Parameters.Add("@ds_id", Ds_ID);

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 获取所有产品调拔从表信息
    /// <param name="Tm_On">盘点单号</param>
    /// </summary>
    /// <returns>返回退货从表信息数据集</returns>
    public DataSet GetRemoveProductChilds(string sTm_No)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_GetRemoveProductChilds", con);

        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        dat.SelectCommand.Parameters.Add("@Tm_no", sTm_No);

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 增加 产品调拔 信息
    /// </summary>
    /// <param> 产品调拔 编号</param>
    /// <param>......</param>
    /// <returns>返回修改是否成功</returns>
/// strTm_no, dtTm_date, strIk_id, strdp_id, strWk_id,strWho_id, strWhi_id, strcr_id, strRemark
    public Boolean AddRemoveProduct(string sTm_no, DateTime dtTm_date, string sIk_id, string sdp_id, string sWk_id, string sWho_id,
                    string sWhi_id, string sCr_id,string sOper,string sRemark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pr_AddRemoveProductM", con);
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@Tm_no", SqlDbType.NVarChar, 20);
        com.Parameters["@Tm_no"].Value = sTm_no;
        com.Parameters.Add("@Tm_date", SqlDbType.DateTime, 8);
        com.Parameters["@Tm_date"].Value = dtTm_date;
        com.Parameters.Add("@Ik_id", SqlDbType.NVarChar, 40);
        com.Parameters["@Ik_id"].Value = sIk_id;
        com.Parameters.Add("@dp_id", SqlDbType.NVarChar, 40);
        com.Parameters["@dp_id"].Value = sdp_id;
        com.Parameters.Add("@Wk_id", SqlDbType.NVarChar, 40);
        com.Parameters["@Wk_id"].Value = sWk_id;
        com.Parameters.Add("@Who_id", SqlDbType.NVarChar, 40);
        com.Parameters["@Who_id"].Value = sWho_id;
        com.Parameters.Add("@Whi_id", SqlDbType.NVarChar, 40);
        com.Parameters["@Whi_id"].Value = sWhi_id;
        com.Parameters.Add("@Cr_id", SqlDbType.NVarChar, 40);
        com.Parameters["@Cr_id"].Value = sCr_id;
        com.Parameters.Add("@Oper", SqlDbType.NVarChar, 12);
        com.Parameters["@Oper"].Value = sOper;
        com.Parameters.Add("@Remark", SqlDbType.NVarChar, 255);
        com.Parameters["@Remark"].Value = sRemark;
  
        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    //****************************************入库确认****************************************
    /// <summary>
    /// 增加入库确认信息
    /// </summary>
    /// <param> 产品调拔 编号</param>
    /// <param>......</param>
    /// <returns>返回修改是否成功</returns>
    /// strDm_no, dtDm_date, strPr_id, dDm_money, iDm_Validate, strDm_Writer, strDm_Valier,strCr_Remark
    public Boolean AddIntoValid(string sDm_no, DateTime dtDate, string sPr_id, decimal dDm_money, int iDm_Validate, string sDm_Writer,
        string strDm_Valier, string sRemark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AddIntoValid", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@Dm_no", SqlDbType.NVarChar, 20);
        com.Parameters["@Dm_no"].Value = sDm_no;
        com.Parameters.Add("@dDate", SqlDbType.DateTime, 8);
        com.Parameters["@dDate"].Value = dtDate;
        com.Parameters.Add("@Pr_id", SqlDbType.NVarChar, 60);
        com.Parameters["@Pr_id"].Value = sPr_id;
        com.Parameters.Add("@Dm_money", SqlDbType.Decimal, 28);
        com.Parameters["@Dm_money"].Value = dDm_money;
        com.Parameters.Add("@Dm_Validate", SqlDbType.Int, 1);
        com.Parameters["@Dm_Validate"].Value = iDm_Validate;
        com.Parameters.Add("@Dm_Writer", SqlDbType.NVarChar, 20);
        com.Parameters["@Dm_Writer"].Value = sDm_Writer;
        com.Parameters.Add("@Dm_Valier", SqlDbType.NVarChar, 20);
        com.Parameters["@Dm_Valier"].Value = strDm_Valier;
        com.Parameters.Add("@Remark", SqlDbType.NVarChar, 255);
        com.Parameters["@Remark"].Value = sRemark;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 增加 产品调拔 明细信息
    /// </summary>
    /// <param> 产品调拔 编号</param>
    /// <param>......</param>
    /// <returns>返回修改是否成功</returns>
    public Boolean AddChildRemoveProduct(string sTm_no, string sIm_id, int iChildAcount, decimal dChildPrice,
        string sBatchNo, string sIdentifyNo, string sRemark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AddRemoveProductD", con);
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@Tm_no", SqlDbType.NVarChar, 20);
        com.Parameters["@Tm_no"].Value = sTm_no;
        com.Parameters.Add("@Im_id", SqlDbType.NVarChar, 20);
        com.Parameters["@Im_id"].Value = sIm_id;
        com.Parameters.Add("@ChildAcount", SqlDbType.Int, 4);
        com.Parameters["@ChildAcount"].Value = iChildAcount;
        com.Parameters.Add("@ChildPrice", SqlDbType.Decimal, 28);
        com.Parameters["@ChildPrice"].Value = dChildPrice;
        com.Parameters.Add("@lot_no", SqlDbType.NVarChar, 30);
        com.Parameters["@lot_no"].Value = sBatchNo;
        com.Parameters.Add("@oo_no", SqlDbType.NVarChar, 30);
        com.Parameters["@oo_no"].Value = sIdentifyNo;
        com.Parameters.Add("@Remark", SqlDbType.NVarChar, 255);
        com.Parameters["@Remark"].Value = sRemark;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 作废 产品调拔主表 信息
    /// </summary>
    /// <param> 产品调拔 编号</param>
    /// <param name="Is_Visible">作废状态1为临时作废，－1为彻底作废</param>
    /// <returns>返回作废是否成功</returns>
    public Boolean DeleteRemoveProduct(string sTm_no, int iIs_Visible)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_DeleteRemoveProductM", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@Tm_no", SqlDbType.NVarChar, 20);
        com.Parameters["@Tm_no"].Value = sTm_no;
        com.Parameters.Add("@Is_Visible", SqlDbType.Int, 1);
        com.Parameters["@Is_Visible"].Value = iIs_Visible;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 作废 产品调拔从表 信息
    /// </summary>
    /// <param> 产品调拔 编号</param>
    /// <param name="Is_Visible">作废状态1为临时作废，－1为彻底作废</param>
    /// <returns>返回作废是否成功</returns>
    public Boolean DeleteChildRemoveProduct(string sTm_no, string sChildDm_no, int iIs_Visible)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_DeleteRemoveProductD", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@Tm_no", SqlDbType.NVarChar, 20);
        com.Parameters["@Tm_no"].Value = sTm_no;
        com.Parameters.Add("@ChildDm_no", SqlDbType.NVarChar, 20);
        com.Parameters["@ChildDm_no"].Value = sChildDm_no;
        com.Parameters.Add("@Is_Visible", SqlDbType.Int, 1);
        com.Parameters["@Is_Visible"].Value = iIs_Visible;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 判断产品调拨是否可以确认：明细表有记录则可以确认，反之不可以确认
    /// </summary>
    /// <param name="Tm_No">单号</param>
    /// <param name="State"></param>
    /// <param name="Auditor">操作员</param>
    /// <returns>操作是否成功</returns>
    public string WhetherAuditRemoveProduct(string sTm_No)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_WhetherAuditRemoveProductM", con);
        com.CommandType = CommandType.StoredProcedure;

        //com.Parameters.Add("@tm_type", SqlDbType.NVarChar, 2);
        //com.Parameters["@tm_type"].Value = sTm_Type;
        com.Parameters.Add("@tm_no", SqlDbType.NVarChar, 30);
        com.Parameters["@tm_no"].Value = sTm_No;

        try
        {
            con.Open();
            string strReturn = (string)com.ExecuteScalar();
            return strReturn;
        }
        catch
        {
            return null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    ///// <summary>
    ///// 确认 产品调拔 信息
    ///// </summary>
    ///// <param> 产品调拔 编号</param>
    ///// <param name="AuditStat">确认状态</param>
    ///// <param name="Auditor">确认人</param>
    ///// <returns>返回确认是否成功</returns>
    //public Boolean AuditStatRemoveProduct(string ID, int AuditStat, string Auditor)
    //{
    //    SqlConnection con = new SqlConnection(strSqlConnectionString);
    //    SqlCommand com = new SqlCommand("Pro_AudittbhTranM_Remove", con);//Pro_AuditStatRemoveProduct Pro_AudittbhTranM

    //    com.CommandType = CommandType.StoredProcedure;
    //    com.Parameters.Add("@tm_no", SqlDbType.NVarChar, 20);
    //    com.Parameters["@tm_no"].Value = ID;
    //    com.Parameters.Add("@dm_state", SqlDbType.Int);
    //    com.Parameters["@dm_state"].Value = AuditStat;
    //    com.Parameters.Add("@auditor", SqlDbType.NVarChar, 60);
    //    com.Parameters["@auditor"].Value = Auditor;
    //    com.Parameters.Add("@type", SqlDbType.NVarChar, 2);
    //    com.Parameters["@type"].Value = "08";

    //    try
    //    {
    //        con.Open();
    //        com.ExecuteNonQuery();

    //        return true;
    //    }
    //    catch
    //    {
    //        return false;
    //    }
    //    finally
    //    {
    //        if (con.State == ConnectionState.Open)
    //        {
    //            con.Close();
    //        }
    //    }
    //}

    /// <summary>
    /// 检查 移库单 是否可以确认：依据（库存中是否有相应记录，并且出库数量小于或等于该商品期末数量）
    /// <returns>CREATE BY KIDY ON 20061211</returns>
    /// </summary>
    public String CheckWhetherAudtitRemove(string sTm_No, string sTm_Type, int iState, string Auditor)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AudittbhTranM_RemoveStore", con); //Pro_AudittbhTranM_Remove
        com.CommandType = CommandType.StoredProcedure;
        SqlParameter ParOutPut = new SqlParameter();

        com.Parameters.Add("@dm_no", SqlDbType.NVarChar, 30);
        com.Parameters["@dm_no"].Value = sTm_No;
        com.Parameters.Add("@dm_state", SqlDbType.Int, 4);
        com.Parameters["@dm_state"].Value = iState;
        com.Parameters.Add("@auditor", SqlDbType.NVarChar, 60);
        com.Parameters["@auditor"].Value = Auditor;
        com.Parameters.Add("@type", SqlDbType.NVarChar, 2);
        com.Parameters["@type"].Value = sTm_Type;
        ParOutPut = com.Parameters.Add("@reIm_no", SqlDbType.NVarChar, 30);
        ParOutPut.Direction = ParameterDirection.Output;
        ParOutPut = com.Parameters.Add("@relot_no", SqlDbType.NVarChar, 30);
        ParOutPut.Direction = ParameterDirection.Output;
        ParOutPut = com.Parameters.Add("@returnState", SqlDbType.NVarChar, 2);
        ParOutPut.Direction = ParameterDirection.Output;
        try
        {
            con.Open();
            com.ExecuteNonQuery();
            string strState = com.Parameters["@returnState"].Value.ToString();//返回状态（N:未入库;Q:库存不足）
            string strReIm_no = com.Parameters["@reIm_no"].Value.ToString();//返回的商品号
            string strReLot_no = com.Parameters["@relot_no"].Value.ToString();//返回的批号
            if (strState == "N")
            {
                return "N" + "|" + strReIm_no + "|" + strReLot_no;
            }
            else if (strState == "Q")
            {
                return "Q" + "|" + strReIm_no + "|" + strReLot_no;
            }
            else
            {
                return "0";
            };
        }
        catch
        {
            return "";
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 修改 产品调拔 信息
    /// </summary>
    /// <param> 产品调拔 编号</param>
    /// <param>......</param>
    /// <returns>返回修改是否成功</returns>
    /// /// strTm_no, dtTm_date, strIk_id, strdp_id, strWk_id,strWho_id, strWhi_id, strcr_id, strRemark
    public Boolean UpDateRemoveProduct(string sTm_no, DateTime dtTm_date, string sIk_id, string sdp_id, string sWk_id, string sWho_id,
                    string sWhi_id, string sCr_id,string sRemark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_UpDateRemoveProductM", con);
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@Tm_no", SqlDbType.NVarChar, 20);
        com.Parameters["@Tm_no"].Value = sTm_no;
        com.Parameters.Add("@Tm_date", SqlDbType.DateTime, 8);
        com.Parameters["@Tm_date"].Value = dtTm_date;
        com.Parameters.Add("@Ik_id", SqlDbType.NVarChar, 40);
        com.Parameters["@Ik_id"].Value = sIk_id;
        com.Parameters.Add("@dp_id", SqlDbType.NVarChar, 40);
        com.Parameters["@dp_id"].Value = sdp_id;
        com.Parameters.Add("@Wk_id", SqlDbType.NVarChar, 40);
        com.Parameters["@Wk_id"].Value = sWk_id;
        com.Parameters.Add("@Who_id", SqlDbType.NVarChar, 40);
        com.Parameters["@Who_id"].Value = sWho_id;
        com.Parameters.Add("@Whi_id", SqlDbType.NVarChar, 40);
        com.Parameters["@Whi_id"].Value = sWhi_id;
        com.Parameters.Add("@Cr_id", SqlDbType.NVarChar, 40);
        //com.Parameters["@Cr_id"].Value = sCr_id;
        com.Parameters["@Cr_id"].Value = "C_200610583440";
        com.Parameters.Add("@Remark", SqlDbType.NVarChar, 255);
        com.Parameters["@Remark"].Value = sRemark;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 修改 产品调拔 明细信息
    /// </summary>
    /// <param> 产品调拔 编号</param>
    /// <param>......</param>
    /// <returns>返回修改是否成功</returns>
    //ctbRs0.Text.Trim(), ctsbRs0.Text.Trim(), ctsbRs1.Text.Trim(), ctsbRs2.Text.Trim(),
    //int.Parse(drpCancel.SelectedValue), int.Parse(drpStop.SelectedValue), ctsbWriter.Text.Trim(), ctbRemark.Text.Trim()
    public Boolean UpDateRemoveProductD(string sTm_no, string sIm_id, decimal iChildAcount,
        decimal dChildPrice, string sBatchNo, string sIdentifyNo, string sRemark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_UpDateRemoveProductD", con);
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@Tm_no", SqlDbType.NVarChar, 20);
        com.Parameters["@Tm_no"].Value = sTm_no;
        com.Parameters.Add("@Im_id", SqlDbType.NVarChar, 20);
        com.Parameters["@Im_id"].Value = sIm_id;
        com.Parameters.Add("@ChildAcount", SqlDbType.Decimal, 28);
        com.Parameters["@ChildAcount"].Value = iChildAcount;
        com.Parameters.Add("@ChildPrice", SqlDbType.Decimal, 28);
        com.Parameters["@ChildPrice"].Value = dChildPrice;
        com.Parameters.Add("@lot_no", SqlDbType.NVarChar, 30);
        com.Parameters["@lot_no"].Value = sBatchNo;
        com.Parameters.Add("@oo_no", SqlDbType.NVarChar, 30);
        com.Parameters["@oo_no"].Value = sIdentifyNo;
        com.Parameters.Add("@Remark", SqlDbType.NVarChar, 255);
        com.Parameters["@Remark"].Value = sRemark;
        

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 检查 调拨出库,调拨入库 是否可以确认：依据（库存中是否有相应记录，并且出库数量小于或等于该商品期末数量）
    /// <returns>CREATE BY KIDY ON 20061208</returns>
    /// </summary>
    public String CheckWhetherAudtit(string sTm_No, string sTm_Type, int iState, string Auditor)
    {

        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AudittbhTranM_RemoveTt", con);
        com.CommandType = CommandType.StoredProcedure;
        SqlParameter ParOutPut = new SqlParameter();

        com.Parameters.Add("@dm_no", SqlDbType.NVarChar, 30);
        com.Parameters["@dm_no"].Value = sTm_No;
        com.Parameters.Add("@dm_state", SqlDbType.Int, 4);
        com.Parameters["@dm_state"].Value = iState;
        com.Parameters.Add("@auditor", SqlDbType.NVarChar, 60);
        com.Parameters["@auditor"].Value = Auditor;
        com.Parameters.Add("@type", SqlDbType.NVarChar, 2);
        com.Parameters["@type"].Value = sTm_Type;
      
        try
        {
            con.Open();
            com.ExecuteNonQuery();
           
                return "0";
           
        }
        catch
        {
            return "1";
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }
}

